﻿using System;
using System.Data;
using System.Windows;
using System.Windows.Input;
using Billing.Controls;
using Billing.Utilities;
using System.Drawing.Printing;

namespace Billing.Windows
{
    public partial class DeliverBulkOrder : Window
    {
        bool isValid = false;

        public DeliverBulkOrder()
        {
            InitializeComponent();
            billNo.Focus();
        }

        private void Window_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Enter && billNo.IsFocused && billNo.Text != string.Empty)
                {
                    DataSet dsDetails = Database.GetBulkOrderDetails(billNo.Text);

                    if (dsDetails != null)
                    {
                        //DateTime deliveryDate = DateTime.Parse(dsDetails.Tables[0].Rows[0]["DeliveryDate"].ToString());
                        //int index = 0;

                        //for (int cnt = 0; cnt < 5; cnt++)
                        //{
                        //    dtDeliveryDt.Items.Add(new ComboBoxItem { Tag = deliveryDate.AddDays(cnt), Content = deliveryDate.AddDays(cnt).DayOfWeek.ToString() + "-" + deliveryDate.AddDays(cnt).ToString("dd-MMM-yyyy") });

                        //    if (deliveryDate.Date == deliveryDate.AddDays(cnt).Date && deliveryDate.Month == deliveryDate.AddDays(cnt).Month && deliveryDate.Year == deliveryDate.AddDays(cnt).Year)
                        //    {
                        //        index = cnt;
                        //    }
                        //}
                        //dtDeliveryDt.SelectedIndex = index;

                        customerName.Content = dsDetails.Tables[0].Rows[0]["CustomerName"].ToString();
                        phoneNo.Content = dsDetails.Tables[0].Rows[0]["PhoneNo"].ToString();
                        totalAmount.Content = dsDetails.Tables[0].Rows[0]["TotalAmount"].ToString();
                        additionalDetails.Content = dsDetails.Tables[0].Rows[0]["Description"].ToString();
                        bookedDate.Content = DateTime.Parse(dsDetails.Tables[0].Rows[0]["BookedDate"].ToString()).ToString("dd-MMM-yyyy");
                        advance.Content = dsDetails.Tables[0].Rows[0]["Advance"].ToString();
                        balance.Content = dsDetails.Tables[0].Rows[0]["Balance"].ToString();
                        deliveryDate.Content = dsDetails.Tables[0].Rows[0]["DeliveryDate"].ToString() + "     " + dsDetails.Tables[0].Rows[0]["DeliveryTime"].ToString();
                        deliveryPlace.Content = dsDetails.Tables[0].Rows[0]["DeliveryPlace"].ToString();

                        if (dsDetails.Tables[0].Rows[0]["DiscountByCost"].ToString() != "0")
                        {
                            discount.Content = dsDetails.Tables[0].Rows[0]["DiscountByCost"].ToString();
                            disc.Content = "Discount Amount: ";
                        }
                        else if (dsDetails.Tables[0].Rows[0]["DiscountByPercentage"].ToString() != "0")
                        {
                            discount.Content = dsDetails.Tables[0].Rows[0]["DiscountByPercentage"].ToString();
                            disc.Content = "Discount Percentage: ";
                        }

                        if (dsDetails.Tables[0].Rows[0]["EachBoxCost"].ToString() == "0")
                        {
                            eachBoxCost.Content = "-";
                        }
                        else
                        {
                            eachBoxCost.Content = dsDetails.Tables[0].Rows[0]["EachBoxCost"].ToString();
                        }

                        if (dsDetails.Tables[0].Rows[0]["Qty"].ToString() == "0")
                        {
                            qty.Content = "-";
                        }
                        else
                        {
                            qty.Content = dsDetails.Tables[0].Rows[0]["Qty"].ToString();
                        }
                        
                        lbItems.Items.Clear();

                        foreach (DataRow row in dsDetails.Tables[1].Rows)
                        {
                            BulkOrderControl ctrl = new BulkOrderControl();
                            ctrl.lblCode.Content = row["ItemCode"].ToString();
                            ctrl.lblItem.Content = row["ItemName"].ToString();
                            ctrl.lblQty.Content = row["Qty"].ToString();
                            lbItems.Items.Add(ctrl);
                        }

                        lblMsg.Text = "Press F1 to Deliver Bulk Order";
                        isValid = true;

                        if (bool.Parse(dsDetails.Tables[0].Rows[0]["IsOrderDelivered"].ToString()))
                        {
                            //lblMsg.Text = "Press F2 to Un Deliver Bulk Order";
                            lblMsg.Text = "Bulk Order Already Delivered";
                            isValid = false;
                        }
                        else if (bool.Parse(dsDetails.Tables[0].Rows[0]["IsOrderCancelled"].ToString()))
                        {
                            lblMsg.Text = "Order Cancelled. Cannot Deliver.";
                            isValid = false;
                        }
                    }
                    else
                    {
                        lblMsg.Text = "Invalid Bulk Order Bill No. Please Enter Valid Bill No.";
                        isValid = false;
                    }

                    if (!isValid)
                    {
                        customerName.Content = string.Empty;
                        phoneNo.Content = string.Empty;
                        totalAmount.Content = string.Empty;
                        additionalDetails.Content = string.Empty;
                        bookedDate.Content = string.Empty;
                        lbItems.Items.Clear();
                        //dtDeliveryDt.Items.Clear();
                    }
                }
                else if (e.Key == Key.F1 && isValid && lblMsg.Text == "Press F1 to Deliver Bulk Order")
                {
                    int result = Database.DeliverBulkOrder(billNo.Text, DateTime.Now);
                    if (result < 0)
                    {
                        MessageBox.Show("Some Unknown Error Has Occurred.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }
                    else
                    {
                        //MessageBox.Show("Bulk Order Delivered Successfully.", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
                        BulkOrderDeliveryPrinter printer = new BulkOrderDeliveryPrinter(billNo.Text, balance.Content.ToString());
                        printer.printDocument1.PrintController = new StandardPrintController();
                        printer.printDocument1.Print();
                        this.Close();
                    }
                }
                else if ((e.Key == Key.F1) && !isValid)
                {
                    MessageBox.Show("Not a Valid Bill.", "Invalid", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
                //else if (e.Key == Key.Right && billNo.IsFocused)
                //{
                //    TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Next);
                //    UIElement elementWithFocus = Keyboard.FocusedElement as UIElement;
                //    if (elementWithFocus != null)
                //        elementWithFocus.MoveFocus(request);
                //}
                //else if (e.Key == Key.Left && dtDeliveryDt.IsFocused)
                //{
                //    TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Previous);
                //    UIElement elementWithFocus = Keyboard.FocusedElement as UIElement;
                //    if (elementWithFocus != null)
                //        elementWithFocus.MoveFocus(request);
                //}
                else if (e.Key == Key.Escape)
                {
                    this.Close();
                }
            }
            catch (Exception) { }
        }

        private void dtDeliveryDt_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Right || e.Key == Key.Left)
                e.Handled = true;
            else
                e.Handled = false;
        }
    }
}
